﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Configuration;

namespace ACAPM.Common
{
    public class EmailHelper
    {
        public static bool SendMail(Email mail)
        {
            try
            {
                //Send mail
                MailMessage message = new MailMessage();
                message.Priority = MailPriority.High;
                message.From = new MailAddress(mail.FromAddress);
                message.To.Add(new MailAddress(mail.ToAddress));
                message.Subject = mail.Subject;
                message.Body = mail.Body;
                message.IsBodyHtml = mail.IsHtml;


                SmtpClient emailClient = new SmtpClient(mail.MailClientAddress);                
                emailClient.UseDefaultCredentials = false;
                emailClient.Credentials = new System.Net.NetworkCredential(mail.Username, mail.Password);
                emailClient.Port = mail.PortNo;
                emailClient.EnableSsl = mail.EnableSsl;
                emailClient.Send(message);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }

    public class Email
    {
        public Email()
        {
            _fromAddress = ConfigurationManager.AppSettings["FromAddress"];
            _toAddress = ConfigurationManager.AppSettings["ToAddress"];
            _subject = ConfigurationManager.AppSettings["Subject"];
            _body = string.Empty;
            _isHtml = Convert.ToBoolean(ConfigurationManager.AppSettings["IsHtml"]);
            _mailClientAddress = ConfigurationManager.AppSettings["MailClientAddress"];
            _portNo = Convert.ToInt32(ConfigurationManager.AppSettings["PortNo"]);
            _enableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
            _username = ConfigurationManager.AppSettings["Username"];
            _password = ConfigurationManager.AppSettings["Password"];
        }

        private string _fromAddress;
        private string _toAddress;
        private string _subject;
        private string _body;
        private bool _isHtml;
        private string _mailClientAddress;
        private int _portNo;
        private bool _enableSsl;
        private string _username;
        private string _password;


        public string FromAddress
        {
            get { return _fromAddress; }
            set { _fromAddress = value; }
        }
        public string ToAddress
        {
            get { return _toAddress; }
            set { _toAddress = value; }
        }
        public string Subject
        {
            get { return _subject; }
            set { _subject = value; }
        }
        public string Body
        {
            get { return _body; }
            set { _body = value; }
        }

        public bool IsHtml
        {
            get { return _isHtml; }
            set { _isHtml = value; }
        }

        public string MailClientAddress
        {
            get { return _mailClientAddress; }
            set { _mailClientAddress = value; }
        }

        public int PortNo
        {
            get { return _portNo; }
            set { _portNo = value; }
        }

        public bool EnableSsl
        {
            get { return _enableSsl; }
            set { _enableSsl = value; }
        }

        public string Username
        {
            get { return _username; }
            set { _username = value; }
        }

        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }
    }
}
